package com.zhoulz.homework.chaptor02.config;

import io.netty.util.CharsetUtil;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.charset.Charset;
import java.util.Properties;

import com.zhoulz.homework.chaptor02.utils.Util;

/**
 * 配置类
 * @author zhoulz
 *
 */
public class Config {
	public static Properties p = new Properties();
	// 配置全局编码
	public static final Charset ENCODING = CharsetUtil.UTF_8;
	
	// zookeeper配置文件
	public static final String LOCAL_CONFIG_NAME = "zookeeper";
	// 本地leader配置文件
	public static final String LOCAL_LEADER_CONFIG_NAME = "leader";
	
	// 配置key
	public static final String KEY_ZK_SERVER_IP = "zk.server.ip";
	public static final String KEY_ZK_SERVER_PORT = "zk.server.port";
	public static final String KEY_TCP_HOST_IP = "tcp.host.ip";
	public static final String KEY_TCP_HOST_PORT = "tcp.host.port";
	public static final String KEY_TCP_KEEPALIVE = "tcp.keepalive";
	
	// 数据存放目录
	public static final String DATA_DIR = Config.class.getResource("/").getFile().replaceFirst("\\w+/$", "data");
	public static void main(String[] args) {
		
		System.out.println(Math.round((float)221/222*100));
	}
	
	public static void load(String path){
		InputStream in = null;
		try {
			if(!path.startsWith("/")){
				path = "/".concat(path);
			}
			if(path.indexOf(".") == -1){
				path = path.concat(".properties");
			}
			in = Config.class.getResourceAsStream(path);
			p.load(in);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	public static String get(String key){
		return p.getProperty(key);
	}
}
